//sws_server.cpp
//patrick braga-henebry
//mark pasquier
//due 28 feb 2008

//normal stuff
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

//networks stuff
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

//C++ stuff
#include <vector>
#include <iostream>
using namespace std;

#define BUFLEN 1024
#define RESET_VAL -22

int main(int argc, char *argv[])
{
	int packet_num;
	srand(time(NULL));
	int sockfd;
	int drop_pct;
	int packet_flag = 0;
	struct sockaddr_in own_addr;
	struct sockaddr_in from_addr; 
	socklen_t from_len;
	int nbytes;
	char recv[BUFLEN];
	vector<int> msg_buffer;

	//open
	if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
		perror("socket");
		exit(1);
	}

	//IP address quick-release
	int sockopt_num=1;
	if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&sockopt_num,sizeof(int)) == -1)
	{  	perror("setsockopt");
    	exit(1);
	} 
	
	//getopt declarations - to parse CLI options
	int input = 0;
	int dflag = 0;
	int drop_thres = 10;
	int portnum;
	//getopt
	while ((input = getopt (argc, argv, "f:p:d")) != -1)
        switch (input)
		{
		case 'd':
			cout << "d on!" << endl;
			dflag = 1;
			break;
		case 'p':
			   portnum = atoi(optarg);
			   break;
		case 'f':
			drop_thres = atoi(optarg);
			if (drop_thres<0 || drop_thres>100)
			{
		        cerr << "drop_percent must be between 0 and 100 " << endl;
				exit(1);
			}
			break;
		case '?':
			cerr << "Usage: " << argv[0] << " [-f percent of packets to drop (default=10)] [-p portnumber] [-d]" << endl;
			return 1;
		default:
			abort ();
		}
	//getopt end

	//struct fillup
	own_addr.sin_family = AF_INET;		 
	own_addr.sin_port = htons(portnum);	 
	own_addr.sin_addr.s_addr = INADDR_ANY;

	//bind
	if (bind(sockfd, (struct sockaddr *)&own_addr, sizeof own_addr) == -1) {
		perror("bind");
		exit(1);
	}

	from_len = sizeof from_addr;
	while(true)
	{
		drop_pct = rand() % 100; //to drop %age of packets
		cout << "waiting..." << packet_flag << endl;
		if ((nbytes = recvfrom(sockfd, recv, BUFLEN-1 , 0, (struct sockaddr *)&from_addr, &from_len)) == -1) {
			perror("recvfrom");
			exit(1);
		}
		packet_num = atoi(recv);
		//test rand against threshold
		if(! (drop_pct >= drop_thres) )
			continue;
		
		for(int i = 0; i < msg_buffer.size(); i++)
		{	//has packet already been received?
			if(msg_buffer[i] == packet_num)
			{
				packet_flag = 1;
				//cout << "msg already received" << endl; 
				//continue;
			}
			else if (packet_num == RESET_VAL) 
			{
				msg_buffer.clear();
				cout << "clearing buffer" << endl;
				packet_flag = RESET_VAL;
				//client does this when done
			}
		}	
		
		if(packet_flag == 1)
		{
			cout << "msg already received" << endl; 
			packet_flag = 0;
		}
		else if(packet_flag == RESET_VAL) 
		{
			packet_flag = 0;
			continue;
		}
		else
		{
			packet_flag = 0;
			//now push
			msg_buffer.push_back(packet_num);
			cout <<"ACK: " << packet_num << endl;
			sendto(sockfd, recv, strlen(recv), 0,(struct sockaddr *)&from_addr, sizeof from_addr);
		}
	
	}

	close(sockfd);

	return 0;
}
